﻿using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;

public enum GameSate
{
    StartGame,
    Gameing,
    GameEnd
}

public class GameManager : MonoBehaviour {
    public static GameManager Instance;
    public UnityEngine.Events.UnityEvent startGame;
    public UnityEngine.Events.UnityEvent restartGame;
    public GameSate gameSate = GameSate.StartGame;
    public List<GameObject> allEnemys=new List<GameObject>();
    public List<GameObject> allTowers=new List<GameObject>();
    public int coin=100;
    public int helth=5;
    public int maxHelth=5;
    public int enemys;
    public Action<int> onCoinChange;  
    public Action<int> onHpChange;
    public Action<float> onEndGame;
    public Action<int,int> onEnemyCountChange;

    private void Awake()
    {
        Instance = this;
        helth = maxHelth;
    }
    private void Start()
    {
        LevelManager.Instance.changeLevel += ChangeLevel;
    }
    #region 玩家属性方法

    public bool UseCoin(int count){
        if(count>coin){
            return false;
        }
        coin-=count;
        if(onCoinChange!=null){
            onCoinChange(coin);
        }
        return true;
    }
    public void AddCoin(int count){
        coin+=count;
        if(onCoinChange!=null){
            onCoinChange(coin);
        }
    }
    public void SetCoin(int coin)
    {
        this.coin = coin;
        if (onCoinChange != null)
        {
            onCoinChange(coin);
        }
    }
    public void SetUserHp(int hp)
    {
        helth = Mathf.Min(hp, maxHelth);
        if (onHpChange != null)
        {
            onHpChange(helth);
        }
    }

    public void UserDamage(int atk){
        helth=Mathf.Max(helth-atk,0);
        if(onHpChange!=null){
            onHpChange(helth);
        }
        if (helth == 0)
        {
            EndGame();
         
        }
        //发射特效
        ObjectFactory.CreatEffect("UserDamage",(o)=> { o.transform.position = new Vector3(350, 700, 0); });
    }
    #endregion


    #region Enemys
    public void AddEnemy(GameObject g){
        allEnemys.Add(g);
    }
    public void RemoveEnemy(GameObject g){
        allEnemys.Remove(g);
        enemys--;
        enemys =Mathf.Max(enemys,0);
        
        if (onEnemyCountChange!=null){
            onEnemyCountChange(enemys,LevelManager.Instance.GetCurrLevelEnemyCount);
        }
        if (enemys == 0)
        {
            if (onEndGame != null)
            {
                onEndGame(helth / maxHelth);
            }
        }

    }
    public void ClearEnemy()
    {
        for (int i = 0; i < allEnemys.Count; i++)
        {
            Destroy(allEnemys[i]);
        }
        allEnemys.Clear();
    }
    void InitEnemyCount()
    {
        if (onEnemyCountChange != null)
        {
            int count = LevelManager.Instance.GetCurrLevelEnemyCount;
            onEnemyCountChange(count, count);
        }
    }
    #endregion


    #region Tower
    public void AddTower(GameObject g)
    {
        allTowers.Add(g);
    }
    public void RemoveTower(GameObject g)
    {
        allTowers.Remove(g);
    }
    public void ClearTower()
    {
        for (int i = 0; i < allTowers.Count; i++)
        {
            Destroy(allTowers[i]);
        }
        allTowers.Clear();

    }
    #endregion

    #region Level
    public void EnterNextLevel()
    {
        LevelManager.Instance.EnterNextLevel();

        Debug.Log("下一关");
    }


    private void ChangeLevel(LevelInfo obj)
    {
        enemys=obj.enemys.Count;
        if(onEnemyCountChange!=null){
            onEnemyCountChange(obj.enemys.Count,enemys);
        }

    }

    #endregion


    #region GameState
    public void StartGame()
    {
        if (startGame!=null)
        {
            startGame.Invoke();
        }
        gameSate = GameSate.Gameing;
        InitEnemyCount();
    }
    

    public void RestartGame()
    {
        if (restartGame != null)
        {
            restartGame.Invoke();
        }
        ClearEnemy();
        ClearTower();
        InitEnemyCount();
        SetUserHp(5);
        SetCoin(100);
        gameSate = GameSate.Gameing;
        LevelManager.Instance.ReStartLevel();
    }
    public void EndGame()
    {
        if (onEndGame != null)
        {
            onEndGame(helth/maxHelth);
        }
        gameSate = GameSate.GameEnd;
        ClearEnemy();
        ClearTower();
    }

    public void QuitGame()
    {
        Application.Quit();
    }
    #endregion
}
